package com.microservice.dbandcache.config;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.microservice.dbandcache.mapper")
//用于指定扫描的mapper接口所在的包，读取org.springframework.core.env.Environment实例
public class MyBatisConfig {
    @Autowired
    private Environment env;
    @Bean
    public DataSource dataSource()throws Exception{
        Properties props =new Properties();
        props.put("driverClassName",env.getProperty("microservicedb1.jdbc.driverClassName"));
        props.put("url",env.getProperty("microservicedb1.jdbc.url"));
        props.put("username",env.getProperty("microservicedb1.jdbc.username"));
        props.put("password",env.getProperty("microservicedb1.jdbc.password"));
        return DruidDataSourceFactory.createDataSource(props);
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
        SqlSessionFactoryBean fb=new SqlSessionFactoryBean();
        fb.setDataSource(ds);
        fb.setTypeAliasesPackage("com.microservice.dbandcache.model");//指定基包
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*lcw/mapping/*.xml"));
        return fb.getObject();
    }
}
